
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.adapter.CouponListAdapter;
import com.kugou.game.sdk.adapter.CouponListAdapterLand;
import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseListAdapter;
import com.kugou.game.sdk.base.BaseListFragment;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.Coupon;
import com.kugou.game.sdk.entity.CouponListResponse;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.Rx;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 描述: 代金券列表Fragment
 *
 * @author jamywang
 * @since 2016年5月19日 下午3:32:49
 */
public class CouponListFragment extends BaseListFragment<Coupon> {

    public static int TYPE_USABLE = 0;

    public static int TYPE_HISTORY = 1;

    private int type = 0;

    private CouponListAdapter mAdapter;

    private CouponListAdapterLand mAdapterLand;

    private User user;

    private View emptyView;

    private TextView tvEmptyTips;

    private ImageView ivEmptyImg;
    
    private boolean isFirstTime = true;

    public static CouponListFragment newInstance(int type) {
        CouponListFragment fragment = new CouponListFragment();
        fragment.setType(type);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        emptyView = LayoutInflater.from(getActivity()).inflate(
                Rx.layout.kg_layout_load_empty_content, null);
        tvEmptyTips = (TextView) emptyView.findViewById(Rx.id.kg_tv_empty);
        ivEmptyImg = (ImageView) emptyView.findViewById(Rx.id.kg_iv_empty);
        ivEmptyImg.setVisibility(View.GONE);
        if (type == 0) {
            tvEmptyTips.setText("您还没有代金券，请留意游戏中心的活动哦~");
        } else if (type == 1) {
            tvEmptyTips.setText("亲，你还没有使用过代金券哦~");
        }
    }

    @Override
    protected int findLayoutId() {
        return Rx.layout.kg_fragment_coupon_listview;
    }

    @Override
    protected BaseListAdapter<Coupon> createAdapter() {
        if (SDKControler.getOritention() == ActivityOrientation.LANDSCAPE) {
            mAdapterLand = new CouponListAdapterLand(mContext, mListView);
            mAdapterLand.setListType(type);
            return mAdapterLand;
        } else {
            mAdapter = new CouponListAdapter(mContext, mListView);
            mAdapter.setListType(type);
            return mAdapter;
        }
    }

    @Override
    protected ArrayList<Coupon> loadDatas() {
        user = UserManager.getInstance().getCurrentUserInternal();
        if (user == null) {
            return null;
        }
        if (type == TYPE_USABLE) {
            CouponListResponse response = UserManager.getInstance().requestUsableCouponList(
                    getPageIndex(), PAGE_SIZE, user.getAccountName());
            if (response != null && response.isUseful()) {
                return response.getCouponList();
            } else {
                return null;
            }
        } else if (type == TYPE_HISTORY) {
            CouponListResponse response = UserManager.getInstance().requestHistoryCouponList(
                    getPageIndex(), PAGE_SIZE, user.getAccountName());
            if (response != null && response.isUseful()) {
                return response.getCouponList();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);
        if (getActivity() == null) {
            return;
        }
        switch (msg.what) {
            case MSG_UI_NO_DATA:
                mTipsLayout.setCustomView(emptyView);
                mTipsLayout.show(TipsLayout.TYPE_CUSTOM_VIEW);
                break;
            default:
                break;
        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    
    public boolean isFirstTime() {
        return isFirstTime;
    }

    public void setFirstTime(boolean isFirstTime) {
        this.isFirstTime = isFirstTime;
    }
    
    @Override
    public void onPause() {
        super.onPause();
        setFirstTime(false);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        if (!isFirstTime()) {
            reLoadData();
        }
    }

}
